package com.tul.exercise.base.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @author tuliang
 */
public class ReflecHelper<T> {

    public  T setValues(Class<T> clazz) {
        T result = null;
        try {
            result = clazz.newInstance();

            List<Field> fieldList = Arrays.asList(clazz.getDeclaredFields());
            for(Field field:fieldList) {
                String name = field.getName();
                Class<?> type = field.getType();

                String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1);

                Method method = clazz.getMethod(methodName, type);
                Object[] value = getValue(clazz, name, type);

                method.invoke(result, value);

                //field.getType().getClass().newInstance();
                //setValues(result, name, );
            }

        }catch (Exception e){

        }

        return result;
    }

    private Object[] getValue(Class<T> clazz, String name, Class<?> type) {

        //if (type.)
            return null;


    }

    public void setProperty(Object obj, String propertyName, Object value) throws Exception {
        // 根据对象获取字节码文件对象
        Class c = obj.getClass();
        // 获取该对象的propertyName成员变量
        Field field = c.getDeclaredField(propertyName);
        // 取消访问检查
        field.setAccessible(true);
        // 给对象的成员变量赋值为指定的值
        field.set(obj, value);
    }
}
